package no.ntnu.fp.testing;

import static org.junit.Assert.*;


import no.ntnu.fp.gui.FactoryProjectPanel;
import no.ntnu.fp.gui.SoftwarePanel;
import no.ntnu.fp.model.Ecu;
import no.ntnu.fp.model.FactoryProject;
import no.ntnu.fp.model.SimpleEcu;
import no.ntnu.fp.model.Software;
import no.ntnu.fp.storage.EcuDbStorage;
import no.ntnu.fp.storage.FactoryDbStorage;
import no.ntnu.fp.storage.SoftwareDbStorage;

import java.sql.Connection;
import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EcuDbStorageTest {
	EcuDbStorage ecu;
	SoftwareDbStorage sdb;
	FactoryDbStorage fdb;
	
	Connection con;
	Connection con2;
	@Before
	public void setUp() throws Exception {
		ecu = new EcuDbStorage();
		sdb = new SoftwareDbStorage();
		fdb  = new FactoryDbStorage();
		
		
		con = ecu.connectToFactoryDb();
		con2 = sdb.connectToFactoryDb();

	}
	


	
	@Test
	public void testEcuDbStorage() {
		assertNotNull(con.toString());
		assertNotNull(con2.toString());
		
		assertTrue(ecu instanceof EcuDbStorage);
	}

	@Test
	public void testOpenSimpleEcu() {
		ArrayList<SimpleEcu> a = ecu.openSimpleEcu();
		for(SimpleEcu s: a){
			assertTrue(s.getEcuId()>0);
			System.out.println("ECU:"+s.getEcuId()+":"+s.getSwId());
			
		}
		
	}

	@Test
	public void testIsUpdated() {

		assertFalse(ecu.isUpdated(new Ecu(1, 0, 0)));
		assertTrue(ecu.isUpdated(new Ecu(1, 2, 0)));
	}

	@Test
	public void testAddEcu() {
		
		ArrayList<SimpleEcu> a = ecu.openSimpleEcu();
		int tmpSize= a.size();
		ArrayList<Software> fa = sdb.openSoftware();
		for(Software s: fa){
			System.out.println("SW:"+s.getSwVersion()+":"+s.getSubVersion());
		}
		System.out.println(sdb.addSoftware(new Software(fa.size()+1, 0, ""), null));
		SimpleEcu s = new SimpleEcu(a.size()+1, fa.size() );
		
		String[] as = ecu.addEcu(s, null);
		System.out.println(as[0]);
		a = ecu.openSimpleEcu();
		int tmp = 0;
		for(SimpleEcu se:a){
			if(se.getEcuId()==s.getEcuId()){
				tmp = 1;
			}
		}
		assertTrue(tmp==1);
		assertTrue(a.size()>tmpSize);
		//System.out.println(a.get(a.indexOf(s)));
		System.out.println(("latest:"+a.get(a.size()-2).getEcuId()));

	}

	@Test
	public void testGetEcuSoft() {
		ArrayList<SimpleEcu> a = ecu.openSimpleEcu();
		SimpleEcu b = a.get(0);
		
		Ecu s = new Ecu(b.getEcuId());
		
		int soft[] = ecu.getEcuSoft(s);
		assertEquals(b.getSwId(), soft[0]);
	}
	
	@After
	public void tearDown() throws Exception {
		
		
	}
}
